package com.ruoyi.web.controller.system;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Point;
import com.ruoyi.system.domain.SysPointExchangeOrder;
import com.ruoyi.system.domain.SysPrizeManagement;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.mongo.SysUserDao;
import com.ruoyi.system.service.ISysPointExchangeOrderService;
import com.ruoyi.system.service.ISysPrizeManagementService;
import com.ruoyi.system.service.PointService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author HP
 * @Date 2021/3/3
 * @Description TODO
 */
@Api("积分兑换礼品记录")
@RestController
@RequestMapping("/system/exchange")
public class SysPointExchangeOrderController {
    @Autowired
    private ISysPointExchangeOrderService iSysPointExchangeOrderService;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private PointService pointService;
    @Autowired
    private ISysPrizeManagementService iSysPrizeManagementService;

    /**
     * 查询兑换订单接口，可以查询全部用户的兑换记录，也可以用user_id查询指定用户的兑换记录
     * @param sysPointExchangeOrder
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/querySysPointExchangeOrder")
    public AjaxResult querySysPointExchangeOrder(SysPointExchangeOrder sysPointExchangeOrder,Integer pageNum,Integer pageSize){
        System.out.println("pageNum==" + pageNum + ",  pageSize==" + pageSize);
        if (pageNum!=null&&pageSize!=null){
            PageHelper.startPage((pageNum / pageSize) + 1,pageSize);
        }
        List<SysPointExchangeOrder> dataList = null;
        //如果用户ID不等于空，查询mongodb，并赋值用户id
        if (!StringUtils.isEmpty(sysPointExchangeOrder.getUser_id())) {
            Users users = sysUserDao.queryUserById(sysPointExchangeOrder.getUser_id());
            //如果是学校管理员, 要查本校所有学生的兑换记录
            if (users.getIsAdmin() == 1) {
                //根据学校ID查询本校的学生列表
                List<Users> stuList = sysUserDao.queryUsersBySchoolId(users.getSchool());
                StringBuffer stuIDs = new StringBuffer();
                for (Users stu : stuList) {
                    //如果已有学生ID添加分隔符 ,号
                    if (stuIDs.length() > 0) {
                        stuIDs.append(",");
                    }
                    //拼学生ID
                    stuIDs.append(stu.getId());
                }
                sysPointExchangeOrder.setUser_id(null); //清空用户ID
                sysPointExchangeOrder.setStuIds(stuIDs.toString()); //并赋值本校所有学生的IDs
                dataList = iSysPointExchangeOrderService.querySysPointExchangeOrder(sysPointExchangeOrder,pageNum,pageSize);
                //赋值每个兑换记录中的学生昵称
                for (SysPointExchangeOrder sysPointExchangeOrdera : dataList) {
                    for (Users student : stuList) {
                        if (sysPointExchangeOrder.getUser_id().equals(student.getId())) {
                            sysPointExchangeOrdera.setNickname(student.getNickname());
                            break;
                        }
                    }
                }
            } else { //否则只查学生or教师自己的兑换记录
                dataList = iSysPointExchangeOrderService.querySysPointExchangeOrder(sysPointExchangeOrder,pageNum,pageSize);
                for (SysPointExchangeOrder sysPointExchangeOrdera : dataList) {
                    sysPointExchangeOrdera.setNickname(users.getNickname());
                }
            }
        } else {
            dataList = iSysPointExchangeOrderService.querySysPointExchangeOrder(sysPointExchangeOrder,pageNum,pageSize);
            for (SysPointExchangeOrder sysPointExchangeOrdera : dataList) {
                //根据用户ID查询用户的昵称
                Users users = sysUserDao.queryUserById(sysPointExchangeOrdera.getUser_id());
                if (users != null) {
                    sysPointExchangeOrdera.setNickname(users.getNickname());
                }
            }
        }
        PageInfo pageInfo = new PageInfo(dataList);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 创建积分兑换订单接口
     * @param sysPointExchangeOrder
     * @return
     */
    @PutMapping("/savePointExchangeOrder")
    public AjaxResult savePointExchangeOrder(SysPointExchangeOrder sysPointExchangeOrder){
        Point point = pointService.queryByUserId(sysPointExchangeOrder.getUser_id());
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        String ord_id = System.currentTimeMillis() + "" + num;
        sysPointExchangeOrder.setOrd_id(ord_id);
        //如果库存不足，插入订单失败
        if (point.getPoint()<sysPointExchangeOrder.getChangecount()*sysPointExchangeOrder.getPoint_num()){
            sysPointExchangeOrder.setPoint_num(sysPointExchangeOrder.getChangecount()*sysPointExchangeOrder.getPoint_num());
            System.out.println("兑换失败");
            sysPointExchangeOrder.setStatus(2);
            iSysPointExchangeOrderService.saveSysPointExchangeOrder(sysPointExchangeOrder);
            return AjaxResult.error();
        }
        sysPointExchangeOrder.setPoint_num(sysPointExchangeOrder.getChangecount()*sysPointExchangeOrder.getPoint_num());
        System.out.println("兑换成功");
        sysPointExchangeOrder.setStatus(1);
        iSysPointExchangeOrderService.saveSysPointExchangeOrder(sysPointExchangeOrder);
        SysPrizeManagement sysPrizeManagement=new SysPrizeManagement();
        sysPrizeManagement.setPid(sysPointExchangeOrder.getProduct_id());
        sysPrizeManagement.setCounts(sysPointExchangeOrder.getChangecount());
        iSysPrizeManagementService.updateByCounts1(sysPrizeManagement);
        Point point1=new Point();
        point1.setPoint(sysPointExchangeOrder.getPoint_num());
        point1.setUser_id(sysPointExchangeOrder.getUser_id());
        pointService.updatePoint(point1);
        return AjaxResult.success();
    }
}
